package main

import (
	"log"
	"net/http"
	"text/template"
)

type Message struct {
	Msg string
}

func home(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		w.Header().Set("Location", "/login")
		w.WriteHeader(http.StatusFound)
	}
}

func login(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("./static/login.html")
	if r.Method == "GET" {
		t.Execute(w, nil)
	} else {
		r.ParseForm()
		account := r.Form["account"][0]
		userPwd := r.Form["password"][0]
		if account == "tantianran" {
			if userPwd == "1qaz#EDC" {
				msg := Message{Msg: "恭喜！验证通过O(∩_∩)O"}
				t.Execute(w, msg)
			} else {
				msg := Message{Msg: "密码错误..."}
				t.Execute(w, msg)
			}
		} else {
			msg := Message{Msg: "用户名错误..."}
			t.Execute(w, msg)
		}

	}
}

func main() {
	http.HandleFunc("/", home)
	http.HandleFunc("/login", login)
	listenAddr := ":80"
	log.Println("ListenAndserve", listenAddr)
	err := http.ListenAndServe(listenAddr, nil)
	if err != nil {
		log.Println(err)
	}
}
